      SUBROUTINE EXEC
C
C-------------------------------------------------------------------------
C  
C  This module opens the scratch files: IMPACT7.ASC and IMPACT10.ASC;
C  opens and reads the Head.ASC, loading the scroll variable arrays 
C  and the plot variable arrays, then closes the file;  and provides the 
C  control for the CADAC simulations under multi-run conditions.  
C  Re-initializing the C variable from tape 90 is also performed in this 
C  module.
C
C--Local Variable Definitions-------------------------------------------
C
C INITSUBS - (I) Flag: 0= Beginning of the trajectory - Execute the 
C            initialization modules; 1= Middle of trajectory - Skip the 
C            initialization modules.  Istage cannot be used for this 
C            because it is modified by the input.
C NPLOTVAR - (I) The number of plot variables selected on the HEAD.ASC 
C            file by the user.
C
C-------------------------------------------------------------------------
C
C---  Storage locations for exec are 50-99
C
C
      COMMON           C(3510)
C 
      COMMON /CCOM/    ICF(25), ICC(25), ICL(25), CA(25), 
     1                 CL(25), CT0(25), NC, ICL2(25)
C
      COMMON /FILEFLG/TRAJBIN, TRAJASC, STATBIN, STATASC, TABOUT, 
     1                RANVAR,  INITASC, INITBIN, TRACKASC, TRACKBIN
      LOGICAL         TRAJBIN, TRAJASC, STATBIN, STATASC, TABOUT, 
     1                RANVAR,  INITASC, INITBIN, TRACKASC, TRACKBIN
C
      COMMON /FILEIDS/ ID_CADIN,   ID_HEAD,    ID_CSAVE,   ID_TABOUT,
     1                 ID_TRAJBIN, ID_TRAJASC, ID_STATBIN, ID_STATASC,
     2                 ID_RANVAR,  ID_IMPACT,  ID_IMPACT7, ID_IMPACT10,
     3                 ID_INITASC, ID_INITBIN, ID_TRACKASC, ID_TRACKBIN
C
      COMMON /FIRSTI/ FINIT
      LOGICAL FINIT
C
      COMMON /FLAG1/   INITGAUSS
      LOGICAL          INITGAUSS
C
      COMMON /KRUN/    JRUN, MULRUN, IGROUP
C
      COMMON /NAD/     IBEEN, IBEGIN, ICARD, ISAVE
C
      COMMON /OINDAT/  J16, ICD, I16
C
      COMMON /OPFLAG/ INTMSG, STGMSGOUT, INECHO, XSWEEP
      LOGICAL         INTMSG, STGMSGOUT, INECHO, XSWEEP
C
      COMMON /PREV/    TPREV, CPPREV, PPPREV
C
      COMMON /STAGE1/  ISTAGE, NUMSTAGE
C
      COMMON /STAGEC/  NAME1(2), NAME2(2)
      CHARACTER        NAME1*6, NAME2*6
C
      DIMENSION  DER(101), V(101)  
C
C
      EQUIVALENCE (C(1801), CRITNO  )
      EQUIVALENCE (C(1802), CRITVAL )
      EQUIVALENCE (C(2000), TIME    )
      EQUIVALENCE (C(2006), ITAP90  )
      EQUIVALENCE (C(2662), HMIN    )
      EQUIVALENCE (C(2663), HMAX    )
      EQUIVALENCE (C(2664), DER(1)  )
      EQUIVALENCE (C(2561), NIP     )
      EQUIVALENCE (C(2765), V(1)    )
      EQUIVALENCE (C(2011), KSTEP   )
      EQUIVALENCE (C(2285), NJ      )
      EQUIVALENCE (C(2280), NV      )
      EQUIVALENCE (C(2001), TSTAGE  )
      EQUIVALENCE (C(2016), PGCNT   )
      EQUIVALENCE (C(2020), LCONV   )
      EQUIVALENCE (C(2866), ICOOR   )
C
C
C      CHARACTER FILENAME*50
C      INTEGER GETENVQQ
      LOGICAL FLAG13
C
C      DATA C/ 3510 * 0.0 /
C
C
C
C---  Open and process the Head.ASC file information.
      CALL RDH1_HEADR( NPLOTVAR )
C
C
C---  Open the output files and start the tabular output by writing the
C---  title, variable headings, etc.
      CALL OP_START( NPLOTVAR )
C
C
C---  Read the input card and load the card data into the modules
      CALL RDI1_INIT_CARDS 
C
C
C
  520 CONTINUE
C
C
C---  Perform a write to the RANVAR output file (if it is requested)
C     to assist in finding variables assignments used in multi-run
C     cases
C
      IF( RANVAR )
     1WRITE(ID_RANVAR,*) ' IGROUP = ', IGROUP, '  JRUN = ', JRUN
C
C---  Very beginning of a trajectory calculation.  
C     This section is the initialization of both Local and  EXEC common 
C     variables.
C
      INITGAUSS = .FALSE. 
C
      NC       = 0
      NUMSTAGE = 0    ! Stage counter.
      J16      = 0    ! Flag: pull data from main trajectory arrays.
      ICD      = 0    ! Main trajectory card counter.
      ISTAGE   = 0
      INITSUBS = 0
      TSTAGE   = 0.0
      TPREV    = 0.0
C
C---  Initialize the C array to zeros:
C
      CALL SET_C_ZERO
C
C---  Setup a default critical value and variable for the non-sweep 
C---  runs.   The input deck for sweep runs will reset these variables
C---  to the proper values.  The default variable for the non-sweep
C--   cases is DHJ = 1773.
C
      CRITNO = 1773 
      CRITVAL = 0.00001 
C
C     Initialize the max and min plot variable arrays.
C
      CALL INI1_PMIN
C
C---  Re- Initialize the common location that contains the number of 
C---  variables in the plot list since this value has been zero-ed out
C---  by the above loop.
      NV = NPLOTVAR 
C
C     Initialize the flag which indicates the action of the EXEC after
C     each integration loop.
C
      KSTEP   = 1
C
C     Initialize several earth and conversion variables.
C
      CALL INI1_VARS 
C
C
C
      IF( ISAVE .GT. 0 ) THEN
C
C         Data has been saved to a tape 90.  Read the data from the 
C         tape.
C
          CALL RD_TAPE90
C
C         Type 90 cards must occur after a stage.  Set the flags
C         to inhibit re-entry into the initialization modules
C         when a simulation is being restored.
C
          ISTAGE   = 16
          INITSUBS = 1
C
C         Set the C variable that indicates when a trajectory is being
C         restarted from a type 90 save; This is set to 0 above (when
C         the C array is initialized)
C
          ITAP90 = 1
C
C         Call the D3I initialization module.  This facilitates the 
C         sweep cases that "start" from a type 90 save.  This ITAP90 
C         variable is used within D3I to inhibit initialization of 
C         certain variables since this is NOT the beginning of the
C         original trajectory.  This call should not interfere with 
C         Non-sweep cases since the D3I module should be in the "DUMMY"
C         modules because the D1I performs the initialization for 
C         non-sweep cases.  Also, these modules should have IF tests
C         on MINIT to exit if an inappropriate case is being called.
C
C         Changed call to D3I to G1I.
C         Since this is in a section of the program that runs TAPE90 
C         with the SWEEP option.  This setup of a TAPE90 and SWEEP has
C         not been fully checked out and will remain an undocumented
C         feature until it has been checked out.  [BC XR97]
C
C         Changed call to G1I with call SWEEPI since the SWEEP initialization
C         routines are now included in the executive routine CADX.FOR [BC 8 Sep 98]
C
          CALL SWEEPI
C
      ENDIF
C
C
C
  580 CONTINUE
C
C---  This section is where the input cards are processed.  The program
C     executes here at the beginning of each trajectory, and returns 
C     here at the end of each stage to process the next set of stage cards.
C
C
      IF( LCONV .LE. 0  ) THEN
C
C         The trajectory has not reached termination as defined by G4 
C         module.
C
C         Perform the input card processing.
C
          CALL OINPT1 
C
          IF( HMIN .EQ. 0.0 ) HMIN = DER(1)
          IF( HMAX .EQ. 0.0 ) HMAX = DER(1)
C
          IF( INITSUBS .EQ. 0 ) THEN
C
C             This is the start of the trajectory computations. 
C             At this point in the code, the variables have been 
C             initialized and the stage 0 cards have been read 
C             and processed.  
C
C             IF( XSWEEP .AND. MULRUN .EQ. 0 ) THEN
C
C                 A Sweep case is being executed (Not with the multi-run 
C                 mode : type 5 card)  Increment the run counter 
C                 here (instead of in the type 21 card processing 
C                 module {prior XR92} ) to keep the count of the number 
C                 of executed trajectories correct.
C
C                  JRUN = JRUN + 1
C              ENDIF
C
              GOTO 800
C
          ELSE
C
C             In the middle of a trajectory either through a stage
C             or a type 90 card.  Insure some values of the Executive
C             common/control variables.
C
              LCONV = 0
              KSTEP = 1
              PGCNT = 1 
C
              INTEST = NV - 1
              NOPLOT = MIN0( 70, MAX0( 0, INTEST) )
              IF( NOPLOT .GT. 0 )  PPCNT = TIME - 0.000001
C
C             Continue with integration cycle.
C
              GO TO 880 
          ENDIF
C
      ELSE
C
C         The end of the trajectory has occurred.  
C         Read the input until an input card type 6 is found.
C
          DO WHILE ( ISTAGE .NE. 6 )
             CALL OINPT2
          ENDDO
C
          GOTO 1060 
C
      ENDIF
C
C
C
C
  800 CONTINUE
C
C     This section performs module initialization.  
C
C---  Execute the pre-initialization modules for the type 1 card 
C     modules.  NOTE:  Currently no modules defined in this module
C
      CALL SUBL1
C
C
C---  Call the initialization modules for the type 2 card modules. 
C
      ICOOR = -2
C
      CALL AUXI
C
C
C     Set the flag indicating that the initialization modules have been
C     executed for this trajectory.
C
      INITSUBS = 1
C
C
C---  Execute the initialization modules for the type 1 cards:
C     Call the initialization modules for the modules specified on the 
C     input type 1 cards ( OUPT2 and STGE2 )
C
      CALL SUBL2
C
C
C---  Load the V and DER variables from the C array: 
C     Calculate the number if integration variables defined.
C
      N  = MAX0( 2, NIP ) 
C
      CALL LD_DERIV( N )
C
  880 CONTINUE 
C
C     This section controls the execution of the modules and the 
C     integration cycle.
C
C---  Execute the auxillary modules (type 2 card modules )
C
      ICOOR = -1
C
C     Call the modules specified on the type 2 cards.
C
      CALL AUXSUB
C
      IF( FINIT ) THEN
C
C     Call the RT initialization routine if a RT INIT file is to be
C     created
C
        CALL OP3_RTIWRITE
        CALL OP3_RTWRITE
        FINIT = .FALSE.
C
      ENDIF
C
C     Check for stage conditions being met.
C     This call statement was removed so that a redundant call to the stage
C     test routine was not called.  The stage routine is called after each 
C     integration cycle is complete.  This call was made immediately after
C     group cards were loaded or after a previous stage [BC 8 Sep 98]
CBC      ICALL = 1
CBC      CALL STGE3( ICALL )
C
C
      IF( NIP .GT. 100 ) THEN
C
          WRITE(ID_TABOUT,940)
  940     FORMAT( '0------ TOO MANY STATE VARIABLES. ', 
     1                         'RUN ABORTED. ------' )
C         Read the input until an input card type 6 is found.
C
          DO WHILE ( ISTAGE .NE. 6 )
             CALL OINPT2
          ENDDO
C
          GOTO 1060 
      ENDIF
C
C
C
  960 CONTINUE
C
C
C---  Execute the integration modules.
C
      N = MAX0( 2, NIP ) 
      N = MIN0( N, 100 )
C
      NJ = N - 1        ! Used in AMRK module
C
      CALL AMRK
C
C
C---  Shift the data produced by the integration arrays into the C arrays.
C
      CALL LD_CARRAY( N )
C
C
C---  Execute the card type 1 modules.
C
      CALL SUBL3
C
C
C---  Check the status of the integration cycle:
C
      IF( KSTEP .EQ. 1 ) THEN
C
C         No change in status - Continue the integration cycle.
C
          GOTO 960 
C
      ELSEIF( KSTEP .EQ. 2 ) THEN  !zzzz
C
C         End of trajectory detected - Read the input until an input 
C         card type 6 is found (incase not already at a type 6 card)
C
          DO WHILE ( ISTAGE .NE. 6 )
             CALL OINPT2
          ENDDO
C
C---  Call STGE3 to write data to SWEEP files at end of total miss trajectory
C
          IF( LCONV .EQ. 5 ) THEN
              ICALL = 1
              CALL STGE3( ICALL )
          ENDIF
C
      ELSEIF( KSTEP .EQ. 3 ) THEN 
C
C         The current stage requirements have been met.
C
          GOTO 580 
C
      ENDIF
C
C
C
C
 1060 CONTINUE
C
C---  This section controls execution when the end of the trajector has 
C---  been detected.
C
C
C---  Reset control flags
      KSTEP = 1
      LCONV = 0
      TPREV = 0.0
C
C---  Zero the integration array.
      DO JV = 2, N
         V(JV) = 0.0  
      END DO
C
C
C---  Check for special execution cases:
C
C
      IF( XSWEEP ) THEN
C
C---       A sweep case is being executed and a trajectory has 
C---       completed.
C
C---       Write the final trajectory data to the TRAJ.* file: ie the 
C---       -1.0 record and the max data record.
           IF( TRAJBIN ) THEN
             IUNIT = ID_TRAJBIN
             CALL OP3_PF11( IUNIT ) 
           ENDIF
           IF( TRAJASC ) THEN
             IUNIT = ID_TRAJASC
             CALL OP3_PF11( IUNIT )
           ENDIF
C
C
           IF( MULRUN .GT. 0   .AND.  JRUN .GE. MULRUN ) THEN 
C
C---           Finished all trajectory runs in the current sweep
C---           position (angle and range setting).  Reset the run 
C---           counter and increment the group counter. 
               JRUN   = 0
               IGROUP = IGROUP + 1
           ENDIF
C
C---       Copy the primary, stored trajectory into the working arrays 
C---       and run the next trajectory sweep.   The card type 18 causes 
C---       the adjustments in the trajectory.   
           CALL WORK
C
C---       Restart for the next trajectory.  Branch to initialization.
           JRUN = JRUN + 1
           GOTO 520
C
      ENDIF
C
C
C---  Write the final trajectory data to the trajectory file: ie the 
C---  -1.0 record and the max data record.
      IF( TRAJBIN ) THEN 
        IUNIT = ID_TRAJBIN
        CALL OP3_PF11( IUNIT ) 
      ENDIF
      IF( TRAJASC ) THEN
        IUNIT = ID_TRAJASC
        CALL OP3_PF11( IUNIT )
      ENDIF        
C
C
      IF( MULRUN .GT. 0 ) THEN
C
C---      Multi-run trajectory case: 
C---      If the run counter is less than the maximum number of runs 
C---      to be perform, then increment run counter and go perform another 
C---      trajectory run.  Branch to the trajectory initialization.
          IF( JRUN .LT. MULRUN ) THEN
              JRUN = JRUN + 1 
              GOTO 520 
          ENDIF
C
C---      Else - Finished all runs in the multi-run case.  Reset the run 
C---      counter.  Reset the read from CSAVE.ASC file to indicate start
C---      of new group trajectory
C
          JRUN   = 1
          ISAVE = 0
C
      ENDIF
C
C---  Trajector(y/ies) for this base card set completed.  Continue 
C---  reading the file to determine if group cards exist, and if so, process 
C---  these trajectories.
C
C---  Increment the group counter.
      IGROUP = IGROUP + 1
C
C---  Copy the primary trajectory to the working arrays, 
C---  read the lead card deck for the cards in the next group and 
C---  modifies/add them to the primary trajectory accordingly.
      CALL RDI2_GROUP( FLAG13 ) 
C
C---  If a card type 13 has not been read (or abnormal end of file 
C---  reached), then a type 12 card at the end of a group card set 
C---  was detected.  Go compute this trajectory.
      IF( .NOT. FLAG13  ) GOTO 520
C
C
C---  A type 13 card was encountered in the input stream.
C     End of simulation:  write the ending records to the trajectory file
C---  and statistics files. Then close the files.
C---  NOTE: Since converted from MS PowerStation 4.0 to Digital Visual FORTRAN
C           the ENDFILE statement was replaced with the CLOSE statement to 
C           correctly disassociate the unit ID from the file without errors
C           for binary files. (The error was produced b/c the file was not opened
C           as a sequential file ?)   [BC 26 May 98]
C
      IF( STATBIN ) THEN
        IUNIT = ID_STATBIN
        CALL OP3_PF11( IUNIT )
        CLOSE( ID_STATBIN )
      ENDIF
      IF( STATASC ) THEN
        IUNIT = ID_STATASC
        CALL OP3_PF11( IUNIT )
        CLOSE( ID_STATASC )
      ENDIF
C
C---  The end records have already been recorded to the trajectory file.
      IF( TRAJBIN )  CLOSE( ID_TRAJBIN )
      IF( TRAJASC )  CLOSE( ID_TRAJASC )
      IF( TRACKBIN ) CLOSE( ID_TRACKBIN )
      IF( TRACKASC ) CLOSE( ID_TRACKASC )
C 
      STOP 
      END
      

